/**
 * Copyright (C) 2009 alex <me@alex.0x08.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



package uber.netbsd.community;

import java.io.Serializable;
import java.util.List;
import java.util.Locale;

import uber.netbsd.community.dao.Person;
import uber.netbsd.community.dao.PersonManager;
import uber.netbsd.community.dao.comment.Comment;
import uber.netbsd.community.dao.comment.CommentManager;
import uber.netbsd.community.dao.gallery.GalleryImage;
import uber.netbsd.community.dao.gallery.GalleryManager;
import uber.netbsd.community.i18n.LocaleInfo;
import uber.netbsd.community.i18n.LocaleStore;

/**
 * User's session support bean
 * 
 * Сессионный бин, подключаемый через jsp:useBean для упрощения жизни с jstl
 *
 * @author alex
 */
public class BzSessionBean extends Loggered implements Serializable {

    /**
     * The so called 'Comment Code' - unique id for a page, used for
     */
    private String commentCode;

    /**
     * Current authenticated user
     */
    private Person currentUser;

    private LocaleInfo currentLocale;

     public List<LocaleInfo> getAvailableLocales() {

        return LocaleStore.getInstance().getSupportedLocales();
    }

    public boolean isLocaleSet() {

        return currentLocale!=null;
    }

    public LocaleInfo getAssociatedLocale() {

        return (currentLocale==null) ? LocaleStore.getInstance().getDefault() :currentLocale;
    }

    public Locale getLocale() {

        return (currentLocale==null) ? LocaleStore.getInstance().getDefault().getLocale() : currentLocale.getLocale();
    }

    public void setLocale(Locale loc) {

        getLogger().debug("__setLocale from obj="+loc);
        if (loc == null) {
            return;
        }

        if (currentLocale==null ||!currentLocale.getLocale().equals(loc)) {

            currentLocale = LocaleStore.getInstance().getInfo(loc);
            getLogger().debug("finish locale="+currentLocale.getDesc());
        }
    }

    public void setLocaleFromString(String code) {

        currentLocale = LocaleStore.getInstance().getInfo(code);
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("set localefromstring="+currentLocale+",code="+code);
        }
    }


    public String getCommentCode() {
        return commentCode;
    }

    public void setCommentCode(String commentCode) {
        this.commentCode = commentCode;
    }

    public void setUserFromLogin(String login) {

        currentUser = PersonManager.getInstance().getPerson(login);
    }

    public Person getCurrentUser() {
        return currentUser;
    }

    /**
     *
     * @return list of images in gallery
     */
    public List<GalleryImage> getAllImages() {

        return GalleryManager.getInstance().getAllGalleryImages();
    }
   
    /**
     *
     * @return list of all registered users
     */
    public List<Person> getAllPeople() {

        return PersonManager.getInstance().getAllPeople();
    }
     /**
      *
      * @return list of all comments with current code
      * current code is set inside taglibs.jsp
      */
     public List<Comment> getComments() {

        return CommentManager.getInstance().getAllComments(commentCode);
    }
}
